package com.sbj.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWTUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbj.dto.req.GoodsReq;
import com.sbj.dto.req.OrderGoodsReq;
import com.sbj.dto.req.SubmitOrderReq;
import com.sbj.dto.res.AddressRes;
import com.sbj.dto.res.GoodsRes;
import com.sbj.dto.res.OrderGoodsRes;
import com.sbj.dto.utils.JWTUtils;
import com.sbj.entity.Goods;
import com.sbj.mapper.GoodsMapper;
import com.sbj.service.GoodsService;
import com.sbj.service.IAddressService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.exception.ResultException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/9 11:58
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {


    private final BeanSearcher beanSearcher;

    @Override
    public Page<GoodsRes> lists(GoodsReq goodsReq) {
        Map<String, Object> build = MapUtils.builder().page(goodsReq.getPageNum() - 1, goodsReq.getPageSize())
                .field(GoodsRes::getCategoryId, goodsReq.getCategoryId()).op(Equal.class)
                .field(GoodsRes::getDeleted, Constants.ZERO).op(Equal.class)
                .field(GoodsRes::getIsListed,Constants.ONE).op(Equal.class)
                .field(GoodsRes::getName, goodsReq.getName()).op(Contain.class).build();
        SearchResult<GoodsRes> search = beanSearcher.search(GoodsRes.class, build);
        Page<GoodsRes> page = new Page<>(goodsReq.getPageNum(), goodsReq.getPageSize());
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }



    @Override
    public List<OrderGoodsRes> jwtToGoods(SubmitOrderReq req) {
        Map<String, Object> map = JWTUtils.parseToken(req.getSign());
        List<OrderGoodsRes> orderGoodsReqs = Convert.toList(OrderGoodsRes.class, map.get("sign"));
        orderGoodsReqs.forEach(orderGoodsReq -> {
            MapBuilder op = MapUtils.builder().field(GoodsRes::getId, orderGoodsReq.getGoodsId()).op(Equal.class);
            GoodsRes goodsRes  = beanSearcher.searchFirst(GoodsRes.class, op.build());
            orderGoodsReq.setImage(goodsRes.getImage());
            orderGoodsReq.setQualityName(goodsRes.getQualityName());
            orderGoodsReq.setName(goodsRes.getName());
            orderGoodsReq.setGoodsPrice(goodsRes.getRuingPrice());
         });
        return orderGoodsReqs;
    }

    @Override
    public void changeListed(GoodsReq goodsReq) {
        Goods goods = this.getById(goodsReq.getId());
        if(ObjectUtil.isEmpty(goods)){
            throw new ResultException("商品不存在");
        }
        goods.setIsListed(goodsReq.getIsListed());
        this.updateById(goods);
    }

    @Override
    public void batchListed(List<GoodsReq> goodsReqList) {
        List<Goods> list=ListUtil.list(true);
        goodsReqList.forEach(goodsReq -> {
            Goods goods=new Goods();
            goods.setIsListed(goodsReq.getIsListed());
            goods.setId(goodsReq.getId());
            list.add(goods);
        });
        this.updateBatchById(list);
    }

}
